package olga.gorbatiuk.journal.impl;

import java.util.Arrays;
import java.util.Date;

import olga.gorbatiuk.journal.Journal;
import olga.gorbatiuk.record.Record;
import olga.gorbatiuk.record.comparators.ByDateAndImportanceComparator;
import olga.gorbatiuk.record.comparators.ByDateComparator;

public class ArrayJournal implements Journal {
    private Record[] recordArray;
    private int size;
    private int capacipity;

    public ArrayJournal() {
	this.size = 0;
	this.capacipity = 0; // пока нет ни одного элемента
	this.recordArray = new Record[capacipity];
    }

    public ArrayJournal(ArrayJournal journal) {
	this.size = journal.getSize();
	this.capacipity = journal.getCapacipity();
	Record[] recordArray = Arrays.copyOf(journal.getRecordArray(), size);
	this.recordArray = recordArray;
    }

    @Override
    public void add(Record record) {
	if (capacipity == size) {
	    capacipity = (capacipity * 3) / 2 + 1;
	    Record[] tempRecordArray = recordArray;
	    recordArray = new Record[capacipity];
	    System.arraycopy(tempRecordArray, 0, recordArray, 0, size);
	}
	recordArray[size++] = record;
    }

    @Override
    public void add(Journal journal) {
	if (journal.getClass() == this.getClass()) {
	    ArrayJournal otherArrayJournal = (ArrayJournal) journal;
	    for (Record record : otherArrayJournal.getRecordArray()) {
		add(record);
	    }

	}
    }

    @Override
    public void remove(Record r) {
	for (int i = 0; i < recordArray.length; i++) {
	    if (recordArray[i].equals(r)) {
		remove(i);
		return;
	    }
	}
    }

    @Override
    public void remove(int index) {
	int numMuoved = size - index - 1;
	System.arraycopy(recordArray, index + 1, recordArray, index, numMuoved);
	recordArray[--size] = null;
    }

    @Override
    public Record get(int index) {
	return recordArray[index];
    }

    @Override
    public void set(int index, Record record) {
	recordArray[index] = record;
    }

    @Override
    public void insert(int index, Record record) {
	if (capacipity == size) {
	    capacipity = (capacipity * 3) / 2 + 1;
	    Record[] tempRecordArray = recordArray;
	    recordArray = new Record[capacipity];
	    System.arraycopy(tempRecordArray, 0, recordArray, 0, size);
	}
	System.arraycopy(recordArray, index, recordArray, index + 1, size
		- index);
	recordArray[index] = record;
	size++;
    }

    @Override
    public void remove(int fromIndex, int toIndex) {
	// TODO Auto-generated method stub

    }

    @Override
    public void removeAll() {
	size = 0;
	capacipity = 0;
	recordArray = new Record[capacipity];
    }

    @Override
    public int size() {
	return size;
    }

    @Override
    public Journal filter(String s) {
	Journal newJournal = new ArrayJournal();
	for (Record record : recordArray) {
	    if (record.toString().equals(s)) {
		newJournal.add(record);
	    }
	}
	return newJournal;
    }

    @Override
    public Journal filter(Date fromDate, Date toDate) {
	// TODO Auto-generated method stub
	return null;
    }

    @Override
    public void sortByDate() {
	Arrays.sort(recordArray, new ByDateComparator());
    }

    @Override
    public void sortByImportanceDate() {
	Arrays.sort(recordArray, new ByDateAndImportanceComparator());
    }

    @Override
    public void sortByImportanceSourceDate() {
	// TODO Auto-generated method stub

    }

    @Override
    public void sortBySourceDate() {
	// TODO Auto-generated method stub

    }

    @Override
    public void printRecords() {
	for (Record record : recordArray) {
	    System.out.println(record.toString());
	}
    }

    public Record[] getRecordArray() {
	return recordArray;
    }

    public void setRecordArray(Record[] recordArray) {
	this.recordArray = recordArray;
    }

    public int getSize() {
	return size;
    }

    public void setSize(int size) {
	this.size = size;
    }

    public int getCapacipity() {
	return capacipity;
    }

}
